//
//  IcRecommendationsMngr.m
//  IceClear
//
//  Created by P & C on 3/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcRecommendationsMngr.h"


@implementation IcRecommendationsMngr

@synthesize usageTypes;
@synthesize facilityTypes;
@synthesize precipitationTypes;
@synthesize drySaltingRates;
@synthesize liquidSaltingRates;
@synthesize materialsManager;
@synthesize recommendationArray;


-(id)initWithDefaults {
	self = [super init];
	
	if (self) {
		// known application rates of dry and liquid salt based on temperature
		// X lbs/100sqm <= Y C
		self->drySaltingRates = [[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithInt:2], 
																	 [NSNumber numberWithInt:6], [NSNumber numberWithInt:10], 
																	 [NSNumber numberWithInt:13], [NSNumber numberWithInt:18], 
																	 [NSNumber numberWithInt:22], [NSNumber numberWithInt:26], 
																	 nil] 
															forKeys:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], 
																	 [NSNumber numberWithInt:-5], [NSNumber numberWithInt:-10], 
																	 [NSNumber numberWithInt:-15], [NSNumber numberWithInt:-20], 
																	 [NSNumber numberWithInt:-25], [NSNumber numberWithInt:-30], 
																	 nil]] retain];
		// X litres/100sqm <= Y C
		self->liquidSaltingRates = [[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithInt:5], 
																	  [NSNumber numberWithInt:7], [NSNumber numberWithInt:10], 
																	  [NSNumber numberWithInt:14], [NSNumber numberWithInt:19], 
																	  [NSNumber numberWithInt:24], [NSNumber numberWithInt:30], 
																	  nil] 
															 forKeys:[NSArray arrayWithObjects:[NSNumber numberWithInt:0], 
																	  [NSNumber numberWithInt:-5], [NSNumber numberWithInt:-10], 
																	  [NSNumber numberWithInt:-15], [NSNumber numberWithInt:-20], 
																	  [NSNumber numberWithInt:-25], [NSNumber numberWithInt:-30], 
																	  nil]] retain];
		
		// The usage types, facility/pavement types and precipitation types used throughout
		// the application are represented as integers with string names associated to them
		
		//												  0			  1
		self->usageTypes = [[NSArray arrayWithObjects:@"De-ice", @"Anti-ice", nil] retain];
		//													   0		  1			 2
		self->facilityTypes = [[NSArray arrayWithObjects:@"Sidewalk", @"Parking", @"Road", nil] retain];
		//														   0			1
		self->precipitationTypes = [[NSArray arrayWithObjects:@"Snowfall", @"Icy Rain", nil] retain];
		
		NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
		NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];
		if (dataRepresentingSavedArray != nil)
		{
			NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
			if (oldSavedArray != nil)
				recommendationArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
			else
				recommendationArray = [[NSMutableArray alloc] init];
		}else {
			recommendationArray = [[NSMutableArray alloc] init];
		}

	}
	
	return self;
}

-(void)saveNewRecommendation:(IcRecommendation *)recommendation{
	[recommendationArray addObject:recommendation];
	[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:recommendationArray] forKey:@"savedArray"];
}


-(NSString *)usageOfType:(NSUInteger)u {
	return [usageTypes objectAtIndex:u];
}

-(NSString *)facilityOfType:(NSUInteger)u {
	return [facilityTypes objectAtIndex:u];
}

-(NSString *)precipitationOfType:(NSUInteger)u {
	return [precipitationTypes objectAtIndex:u];
}

// this method will construct and return a new Recommendation model with all
// the information about what the user should use based on the provided parameters
-(IcRecommendation *)getRecommendationTo:(NSUInteger)use onFacilityOfType:(NSUInteger)facilityType 
						   atTemperature:(NSUInteger)temperature forPrecipitationOfType:(NSUInteger)precipitationType {
	// create a new model object
	IcRecommendation* rec = [[IcRecommendation alloc] initWithType:use onFacilityOfType:facilityType 
													 atTemperature:temperature forPrecipitationOfType:precipitationType];
	
	// ask the Material models manager about materials that have as their characteristics the requirements given
	// to us; i.e. what materials should go with this recommendation
	rec.materials = [materialsManager materialsGoodToUsageType:rec.usageType onFacilityType:rec.facilityType 
												 atTemperature:rec.temperature forPrecipitationType:rec.precipitationType];
	
	return rec;
}

+(id)recommendationsManager {
	IcRecommendationsMngr* mngr = [[IcRecommendationsMngr alloc] initWithDefaults];
	return mngr;
}

-(void)dealloc {
	[usageTypes release];
	[facilityTypes release];
	[precipitationTypes release];
	[materialsManager release];
	[drySaltingRates release];
	[liquidSaltingRates release];
	[recommendationArray release];
	[super dealloc];
}

@end
